/*
 * CSliceAndDice.java
 * 
 * Copyright (c) 2009 Giles Millward (Manigra). All rights reserved.
 * 
 * This file is part of Expression program is undefined on line 8, column 40 in Templates/Licenses/license-gplv3.txt..
 * 
 * Expression program is undefined on line 10, column 19 in Templates/Licenses/license-gplv3.txt. is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Expression program is undefined on line 15, column 19 in Templates/Licenses/license-gplv3.txt. is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Expression program is undefined on line 21, column 30 in Templates/Licenses/license-gplv3.txt..  If not, see <http://www.gnu.org/licenses/>.
 */

package dpssystem;

import dpsstats.CDamageStats;
import dpsstats.CFinisherStats;
import dpssystem.CAttackChance.HitType;

/**
 *
 * @author Mani
 */
public class CSliceAndDice extends CAbility {
    double m_sliceAndDiceTimeArray[] = {0.0, 9.0, 12.0, 15.0, 18.0, 21.0};
    double m_cutToTheChaseChance = 0.0;
    boolean m_cutToTheChaseEnabled = false;
    CDamageStats m_damageStats;
    CFinisherStats m_finisherStats;

    CSliceAndDice (CSim p_sim)
    {
        super(p_sim, true);
        m_energyRequired = 25;
        
        // Slice and Dice glyph adds 3 seconds
        if (p_sim.m_dpsInterface.m_glyphs.m_sliceAndDice)
        {
            for (int i = 1; i < 6; i ++)
            {
                m_sliceAndDiceTimeArray[i] += 3;
            }
        }

        double l_tier6bonus = 0.0;
        if (p_sim.m_dpsInterface.m_bonusSet.isTier6_2pc())
        {
            l_tier6bonus = 0.05;
        }

        // Add on improved slice and dice talent extra duration
        double l_multiplier = 1.0 + (p_sim.m_dpsInterface.m_talents.getImprovedSliceAndDiceDouble() * 25) + 
                l_tier6bonus;

        for (int i = 1; i < 6; i++)
        {
            m_sliceAndDiceTimeArray[i] *= l_multiplier;
        }

        // 0 - 100%
        if (p_sim.m_dpsInterface.m_talents.getCutToTheChase() != 0)
        {
            m_cutToTheChaseChance = p_sim.m_dpsInterface.m_talents.getCutToTheChaseDouble() * 20;
            m_cutToTheChaseEnabled = true;
        }

        m_damageStats = m_sim.m_stats.m_sliceAndDiceStats;
        m_finisherStats = m_sim.m_stats.m_finisherStatsList.get(2);
    }

    public boolean canExecute()
    {
        if (m_sim.m_target.m_comboPoints.m_comboPoints == 0)
        {
            // Need some combo points
            return false;
        }

        if (m_sim.m_player.m_energyRegen.m_currentEnergy < m_energyRequired)
        {
            // Not enough energy
            return false;
        }

        if (m_sim.m_currentRunTime < m_sim.m_gcd.m_endOfGCD)
        {
            // Still on GCD
            return false;
        }

        if (this.m_cutToTheChaseEnabled &&
                m_sim.m_currentRunTime < this.m_endOfTimer)
        {
            return false;
        }

        return true;
    }

    public void execute()
    {
        // Slice and Dice finisher executed
        m_finisherStats.addFinisher(m_sim.m_target.m_comboPoints.m_comboPoints);

        HitType l_hitType = m_sim.m_attackChance.getFinisherAttackHit(m_sim.m_baseStats.getHitChance(), m_sim.m_baseStats.getExpertiseDodge());
        if (l_hitType == HitType.NORMAL)
        {
            this.m_endOfTimer =
                    m_sim.m_currentRunTime + m_sliceAndDiceTimeArray[m_sim.m_target.m_comboPoints.m_comboPoints];
            m_sim.m_gcd.startGCD();
            m_sim.m_player.m_energyRegen.useAbilityEnergy(m_energyRequired);
            m_sim.m_player.m_energyRegen.relentlessStrikes();            
            // Reset combo points AFTER relentless strikes!!
            m_sim.m_target.m_comboPoints.m_comboPoints = 0;
        }
        else
        {
            // Missed!!
            m_sim.m_gcd.startGCD();
            m_sim.m_player.m_energyRegen.useAbilityEnergy(m_energyRequired);
            m_sim.m_target.m_comboPoints.m_comboPoints = 0;            
        }

        m_damageStats.addStats(l_hitType, 0);
    }

    public double getHaste()
    {
        if (m_sim.m_currentRunTime <= this.m_endOfTimer)
        {
            // Slice and dice 40% is up
            return 0.40;
        }
        
        return 0.0;
    }

    // Called via envenom or eviscerate
    public void cutToTheChase()
    {
        if (this.m_cutToTheChaseEnabled)
        {
            // Slice n Dice still up ?
            if (this.m_endOfTimer >= m_sim.m_currentRunTime &&
                m_sim.m_rng.nextDouble() <= this.m_cutToTheChaseChance)
            {
                // Refresh slice n dice
                this.m_endOfTimer =
                    m_sim.m_currentRunTime + m_sliceAndDiceTimeArray[5];
            }
        }
    }
}
